package com.bxd.baodan.list;

import android.view.View;

import com.bumptech.glide.Glide;
import com.google.gson.reflect.TypeToken;
import com.bxd.baodan.App;
import com.bxd.baodan.R;
import com.bxd.baodan.activity.WebViewPager;
import com.bxd.baodan.adapter.ViewHolder;
import com.bxd.baodan.app.JsonApi;
import com.bxd.baodan.base.BaseListFragment;
import com.bxd.baodan.item.ProductItem;
import com.bxd.baodan.item.SortItem;
import com.bxd.baodan.util.NetUtil;
import com.bxd.baodan.util.StringUtil;
import com.bxd.baodan.widget.ShapedImageView;
import com.umeng.analytics.MobclickAgent;

import java.lang.reflect.Type;
import java.util.List;

public class ProductListFragment extends BaseListFragment<ProductItem> {

    SortItem type = new SortItem();//防止空指针

    public static ProductListFragment newInstance(SortItem item) {
        ProductListFragment fragment = new ProductListFragment();
        fragment.type = item;
        return fragment;
    }


    @Override
    protected Type getTypeToken() {
        return new TypeToken<List<ProductItem>>() {
        }.getType();
    }


    @Override
    protected void preDone(View v) {
        if (type.id.equals("select")) {
            listView.addHeaderView(View.inflate(getActivity(), R.layout.product_list_header, null));
        }

        autoRefresh();
    }

    @Override
    protected int getFragmentLayout() {

        return R.layout.pull_refresh_layout_no_divider;
    }


    @Override
    public void setUsualNetUtil() {
        if (type.id.equals("free")) {
            netUtil = new NetUtil(getActivity(), JsonApi.PRODUCT_FREEINSURANCE);
        } else if (type.id.equals("select")) {
            netUtil = new NetUtil(getActivity(), JsonApi.PRODUCT_AIRECOMMEND);
        } else {
            netUtil = new NetUtil(getActivity(), JsonApi.PRODUCT_LISTS);
            netUtil.setParams("crowd", type.id);
        }

    }

    @Override
    protected int getItemLayout() {

        return R.layout.product_item;
    }

    @Override
    protected void convert(ViewHolder holder, final ProductItem item) {
        holder.setText(R.id.p_name, item.p_name);
        holder.setText(R.id.p_features, item.p_features);
        if (StringUtil.isEmpty(item.cat_name)) {
            holder.getView(R.id.cat_name).setVisibility(View.GONE);
        } else {
            holder.setText(R.id.cat_name, item.cat_name);
        }

        if (StringUtil.isEmpty(item.price)) {
            holder.getView(R.id.p_price).setVisibility(View.GONE);
        } else {
            holder.setText(R.id.p_price, item.price);
            holder.getView(R.id.p_price).setVisibility(View.VISIBLE);
        }

        Glide.with(getActivity()).load(item.p_img).
                error(R.drawable.img_error).
                into(((ShapedImageView) holder.getView(R.id.p_image)));

        Glide.with(getActivity()).load(item.p_img).
                error(R.drawable.img_error).
                into(((ShapedImageView) holder.getView(R.id.p_image)));


        holder.getView(R.id.p_view).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //item.p_url= "https://ztg.zhongan.com/promote/entrance/promoteEntrance.do?redirectType=h5&promotionCode=INST171047535012&productCode=PRD160436305017&promoteCategory=single_product&token=";
                MobclickAgent.onEvent(App.getContext(), "v6_1_0_CP_cpxq");
                if (type.id.equals("select")) {
                    if (!StringUtil.isEmpty(item.p_introduction)) {
                        showToast(item.p_introduction);
                    }


                }
                openActivity(WebViewPager.class, new String[]{"startUrl"}, new String[]{item.p_url});
            }
        });


    }


    @Override
    protected String getArrayStr(String json_data) {
        return "list";
    }
}
